package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Logs;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 系统日志
 * 
 */
@Component
public class LogsDao implements BaseDao<Logs> {
    @Override
    public int insert(Logs entity) {
        return DBHelper.executeUpdate("insert into logs (operateor, operatetype, remark, is_status) VALUES (?,?,?,?);",
            entity.getOperateor(), entity.getOperatetype(), entity.getRemark(), entity.getIs_status());
    }

    @Override
    public int update(Logs entity) {
        return DBHelper.executeUpdate("update logs set operateor=?,operatetype=?,remark=?,is_status=? where id=?",
            entity.getOperateor(), entity.getOperatetype(), entity.getRemark(), entity.getIs_status(), entity.getId());
    }

    @Override
    public int delete(Logs entity) {
        return DBHelper.executeUpdate("update logs set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from logs");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        String sql = "select * from logs where 1=1 ";
        if (obj[0] != null && obj[1] != null) {
            sql += " and gmt_create BETWEEN '" + obj[0] + "' AND '" + obj[1] + "' ";
        }
        if (obj[2] != null && !"".equals(obj[2])) {
            sql += " and operateor like '%" + obj[2] + "%'";
        }
        sql += "and is_deleted=0 order by id desc limit ?,?";
        return DBHelper.executeQuery(sql, page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Logs entity) {
        return DBHelper.executeQuery("select * from logs where 1=1 and is_deleted=0 and id=? ", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql = "select count(*) counts from logs where 1=1 ";
        if (obj[0] != null && obj[1] != null) {
            sql += " and gmt_create BETWEEN '" + obj[0] + "' AND '" + obj[1] + "' ";
        }
        if (obj[2] != null && !"".equals(obj[2])) {
            sql += " and operateor like '%" + obj[2] + "%'";
        }
        sql += "and is_deleted=0";
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }
}
